<?php

namespace app\comm\service;
use app\mall\model\Goods;
use app\comm\model\GoodsComment;
use app\comm\model\GoodsMaterial;
use app\comm\model\GoodsYongliao;
use app\comm\model\SysLevel;
use app\comm\model\SysPoint;
use app\comm\model\Coupon;

use ServS\DbShowcollo;
use app\comm\service\GoodsService;

class SysToolsLevePoint{
/*****************           等级规则         *********************************/
	/**
	 * 获取一条积分的方法
	 * @return [type] [description]
	 */
	public function getlevelbyid($id){
		return SysLevel::get($id)->toArray();
	}
	/**
	 * 增加等级
	 */
	public function add_level($post){
		$syslevel = new SysLevel;
		$res = DbShowcollo::add($syslevel,$post);
		return $res;
	}

	/**
	 * 编辑等级
	 */
	public function editor_level($post){
		$syslevel = new SysLevel;
		$postid = $post['id'];
		unset($post['id']);
		$res = DbShowcollo::refresh($syslevel,'id',$postid,$post);
		return $res;
	}
/*****************        积分规则      ***************************/

	/**
	 * 添加一条积分
	 * @param [type] $post [description]
	 */
	public function add_point($post){
		$SysPoint = new SysPoint;
		$res = DbShowcollo::add($SysPoint,$post);
		return $res;
	}

	/**
	 * 获取一条积分
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getpointbyid($id){
		return SysPoint::get($id)->toArray();
	}

	/**
	 * 编辑一条积分
	 */
	public function editor_point($post){
		$syslevel = new SysPoint;
		$postid = $post['id'];
		unset($post['id']);
		$res = DbShowcollo::refresh($syslevel,'id',$postid,$post);
		return $res;
	}


	/**
	 * 金额与积分的变更对比
	 */
	public function jifenjisuan($biangengjifen){
		$syspoint = SysPoint::where('1=1')->order('point desc')->select();
		$od =[];
		foreach ($syspoint as $key => $value) {
			if($biangengjifen>$value->point){
				$od= ['id'=>$value->id,'jifen'=>$value->point,'jifen_cn'=>$value->remark];
				break;
			}
		}
		if(empty($od)){
			$od= ['0'=>0,'jifen'=>0,'jifen_cn'=>'太少没有积分可送'];
		}
		return $od ;
	}
	
/*******************************************优惠券列表***********************************/
	
	/**
	 * 优惠券活动列表
	 * @return [type] [description]
	 */
	public function coupoulist($urlcode,$get=''){
		$goodsservice = new GoodsService;
		$rows=6;
        if($get!==''){
        	$coupon = Coupon::where('name','like','%'.$get.'%' )->order('addtime desc')->paginate($rows, false, ['query' =>$urlcode]);
        }else{
        	$coupon = Coupon::where('1=1')->order('addtime desc')->paginate($rows, false, ['query' =>$urlcode]);
        }
		foreach ($coupon as $key => &$value) {
			if($value->goods_id){
				$value->goods_id=$goodsservice ->getgoodbyid($value->goods_id)['name'];	
			}else{
				$value->goods_id ="通用优惠券";
			}
			$value ->addtime = date('Y-m-d',$value ->addtime);
		}
		return $coupon;
	}

	/**
	 * 优惠券列表 ， 前端展示
	 */
	public function precoupoulist($cord){
		$goodsservice = new GoodsService;
		$coupon = Coupon::where($cord)->paginate(10);
		foreach ($coupon as $key => &$value) {
			if($value->goods_id){
				$value->goods_id=$goodsservice ->getgoodbyid($value->goods_id)['name'];	
			}else{
				$value->goods_id ="通用优惠券";
			}
			$value ->use_start_time = date('Y-m-d',$value ->use_start_time);
			$value ->use_end_time	= date('Y-m-d',$value ->use_end_time);
			$value ->addtime = date('Y-m-d',$value ->addtime);
		}
		return $coupon;
	}

	/**
	 * 获取优惠券，用id
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getbycoupid($id){
		$coupon = Coupon::get($id);
		return $coupon->toArray();
	}

	/**
	 *设为 无效
	 */
	public function setbycoupidustaus($id){
		$coupon = new  Coupon;
		$post['status']= 0;
		$res = DbShowcollo::refresh($coupon,'id',$id,$post);
		return $res;
	}

	/**
	 * 无效跟有效转化
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function setbycoupidustausswitch($id){
		$coupon = new  Coupon;
		$coup  =  $this ->getbycoupid($id);
		if($coup['status']==0){
			$post['status']= 1;
		}else{
			$post['status']= 0;
		}
		$res = DbShowcollo::refresh($coupon,'id',$id,$post);
		return $res;
	}

	/**
	 * 添加优惠券
	 * @return [type] [description]
	 */
	public function addcoupon($post){
		$coupon = new Coupon;
		$data=$post;
		$data['send_start_time']=strtotime($post['send_start_time']);
		$data['send_end_time']=strtotime($post['send_end_time']);
		$data['use_start_time']=strtotime($post['use_start_time']);
		$data['use_end_time']=strtotime($post['use_end_time']);
		if($post['type'] == 1){
			$data['goods_id']=='0';
		}
		$res = DbShowcollo::add($coupon,$data);	
		return $res;
	}


}